﻿using System.IO;

namespace GFramework.Netwrok
{
    public sealed class NetService : Leaf, INetService, IAwake<INetService>
    {
        private INetService m_netService;

        public event GAction<INetService, INetService> NetServiceAccepted
        {
            add
            {
                m_netService.NetServiceAccepted += value;
            }

            remove
            {
                m_netService.NetServiceAccepted -= value;
            }
        }

        public event GAction<INetService, object> NetServiceConnected
        {
            add
            {
                m_netService.NetServiceConnected += value;
            }

            remove
            {
                m_netService.NetServiceConnected -= value;
            }
        }

        public event GAction<INetService> NetServiceClosed
        {
            add
            {
                m_netService.NetServiceClosed += value;
            }

            remove
            {
                m_netService.NetServiceClosed -= value;
            }
        }

        public event GAction<INetService> NetServiceSended
        {
            add
            {
                m_netService.NetServiceSended += value;
            }

            remove
            {
                m_netService.NetServiceSended -= value;
            }
        }

        public event GAction<INetService> NetServiceReceived
        {
            add
            {
                m_netService.NetServiceReceived += value;
            }

            remove
            {
                m_netService.NetServiceReceived -= value;
            }
        }

        public event GAction<INetService, NetErrorCode, int, string> NetServiceError
        {
            add
            {
                m_netService.NetServiceError += value;
            }

            remove
            {
                m_netService.NetServiceError -= value;
            }
        }

        public bool Connected { get { return m_netService.Connected; } }

        public byte NetType { get { return m_netService.NetType; } }

        public void Accept(string address)
        {
            m_netService.Accept(address);
        }

        public void Connect(string address)
        {
            m_netService.Connect(address);
        }

        public void Connect(string address, object userData)
        {
            m_netService.Connect(address, userData);
        }

        public void Close()
        {
            m_netService.Close();
        }

        public void Send(MemoryStream stream)
        {
            m_netService.Send(stream);
        }

        public void Receive(MemoryStream stream)
        {
            m_netService.Receive(stream);
        }

        public void Awake(INetService netService)
        {
            m_netService = netService;
        }

        public override void Clear()
        {
            base.Clear();
            m_netService.Clear();
            m_netService = null;
        }
    }
}
